﻿var http = require("http");
var queryString = require('querystring');
var mysql = require('mysql');
var net = require('net');

var HOST = '192.168.1.104';
var PORT = 6969;

net.createServer(function(socket) {
  // 我们获得一个连接 - 该连接自动关联一个socket对象
  console.log('连接: ' + socket.remoteAddress + ':' + socket.remotePort);

  sock.on('data', function(data) {
    console.log('接收数据 ' + sock.remoteAddress + ': ' + data);
    // 回发该数据，客户端将收到来自服务端的数据
    sock.write('You said "' + data + '"');
  });

  sock.on('close', function(data) {
    console.log('关闭: ' +
      sock.remoteAddress + ' ' + sock.remotePort);
  });


}).listen(PORT, HOST)
console.log("服务器已启动在" + PORT);


/**
 * [插入位置数据]
 * @param  {[Number]} X [纬度]
 * @param  {[Number]} Y [经度]
 * @return {[Number]}   [description]
 */
function insertPosition(X, Y) {
  var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'test'
  });


  connection.connect();

  var sql = 'INSERT INTO position (X,Y) VALUES (' + X + ',' + Y + ')';

  connection.query(sql, function(err, result) {
    if (err) {
      console.log('[SELECT ERROR] - ', err.message);
      return;
    }

    console.log('--------------------------INSERT----------------------------');
    console.log(result);
    console.log('------------------------------------------------------------\n\n');
  });

  connection.end();
}
/**
 * [获取表中所有位置信息]
 * @return {[type]} [description]
 */
function getPosition() {

  var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'test'
  });


  connection.connect();

  var sql = 'SELECT * FROM position';

  connection.query(sql, function(err, result) {
    if (err) {
      console.log('[SELECT ERROR] - ', err.message);
      return;
    }

    console.log('--------------------------SELECT----------------------------');
    console.log(result);
    console.log('------------------------------------------------------------\n\n');

    return result;
  });

  connection.end();
}